/*
 * ChooseNormalInvoiceView.java
 * business
 *
 * Created by ChenTao on 2017/1/7.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.shoppingcart.chooseinvoice.normal;

import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;

import com.yidiandao.business.R;
import com.yidiandao.business.ui.adapter.ChooseNormalInvoiceAdapter;
import com.yidiandao.business.ui.main.shoppingcart.chooseinvoice.ChooseInvoiceFragment;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.view.ListViewForScrollView;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/** 确认订单-选择发票-普通发票 */
public class ChooseNormalInvoiceView
        implements ChooseNormalInvoiceContract.View, TextWatcher, AdapterView.OnItemClickListener {

    private Activity mActivity;

    private ChooseNormalInvoiceContract.Presenter mPresenter;

    private EditText mEtInvoiceHeader;

    private ListViewForScrollView mListView;

    private ChooseNormalInvoiceAdapter mAdapter;

    public ChooseNormalInvoiceView(View normalRoot, Activity activity) {
        mActivity = activity;

        mEtInvoiceHeader = (EditText) normalRoot.findViewById(R.id.et_invoice_header);
        mListView = (ListViewForScrollView) normalRoot.findViewById(R.id.list_view);

        initListView();
        initListeners();
    }

    private void initListeners() {
        mListView.setOnItemClickListener(this);
    }

    private void initListView() {
        mAdapter = new ChooseNormalInvoiceAdapter(mActivity);
        mListView.setAdapter(mAdapter);
    }

    @Override
    public void setPresenter(ChooseNormalInvoiceContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return ChooseInvoiceFragment.class.getSimpleName();
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    public void refreshTitle(String normalTitle, boolean firstIn) {
        mEtInvoiceHeader.setText(normalTitle);
        if (firstIn) {
            mEtInvoiceHeader.addTextChangedListener(this);
        }
    }

    @Override
    public void setInvoiceListItems(List<DisplayListItem> displayListItems) {
        mAdapter.setData(displayListItems);
    }

    @Override
    public String getInputTitle() {
        return mEtInvoiceHeader.getText().toString().trim();
    }

    @Override
    public void finishActivity() {
        mActivity.finish();
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        mPresenter.filterNormalInvoiceList(getInputTitle());
    }

    @Override
    public void afterTextChanged(Editable s) {
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mPresenter.onItemClick(position);
    }
}
